<template>
  <div class="my-tab-bar">
    <div
      class="tab-item"
      v-for="(item, index) in list"
      :key="index"
      :class="{ current: index === activeIndex }"
      @click="clickFn(index, item.componentName)"
    >
      <!-- 图标 -->
      <span class="iconfont" :class="item.iconText"></span>
      <!-- 文字 -->
      <span>{{ item.text }}</span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    list: {
      type: Array,
      required: true, //规定list变量必须要传值
      // 定义检验规则
      validator(value) {
        //value是父组件传过来的内容
        return value.length >= 2 && value.length <= 5; //当父组件传过来的值符合条件返回true
        // 可以不用定义错误时的提示，因为validator有自己的错误提示
      },
    },
  },
  data() {
    return {
      activeIndex: 0,
    };
  },
  methods: {
    clickFn(index, componentName) {
      this.activeIndex = index;
      this.$emit("changeCom", componentName); //给动态切换组件的自定义方法传值并触发
      this.$emit("router", componentName);//给路由切换组件的自定义方法传值并触发
    },
  },
};
</script>

<style lang="less" scoped>
.my-tab-bar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: white;
  .tab-item {
    display: flex;
    flex-direction: column;
    align-items: center;
  }
}

.current {
  color: #1d7bff;
}
</style>